--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

---@type BP_Field_C
local M = UnLua.Class()

local Field = LoadObject("/Script/Paper2D.PaperSprite'/MashRunner/PaperAssets/Background/T_Field_Sprite.T_Field_Sprite'")
local PaperSpriteClass = UE.UPaperSpriteComponent.StaticClass()

-- function M:Initialize(Initializer)
-- end

-- function M:UserConstructionScript()
-- end

function M:ReceiveBeginPlay()
    self:AddPaperSprite()
end

-- function M:ReceiveEndPlay()
-- end

-- function M:ReceiveTick(DeltaSeconds)
-- end

-- function M:ReceiveAnyDamage(Damage, DamageType, InstigatedBy, DamageCauser)
-- end

-- function M:ReceiveActorBeginOverlap(OtherActor)
-- end

-- function M:ReceiveActorEndOverlap(OtherActor)
-- end

--- UK2Node_AddComponent
function M:AddPaperSprite()
    for i = 0, self.StageParts - 1 do
        local Transform = UE.FTransform()
        Transform.Translation = UE.FVector(self.StageLength * i, 0, 0)
        local PaperSprite = self:AddComponentByClass(PaperSpriteClass, false, Transform, false)
        PaperSprite:SetSprite(Field)
    end

    self:UpdateBoxExtentAndLocation(self.BoxTop)
    self:UpdateBoxExtentAndLocation(self.BoxBottom)

    self:UpdateGoalLocation(self.GoalTop)
    self:UpdateGoalLocation(self.GoalBottom)

end

-- 更新盒子的范围
function M:UpdateBoxExtentAndLocation(BoxComponent)
    local BoxSize = UE.FVector((self.StageLength / 2 * self.StageParts), BoxComponent.BoxExtent.Y, BoxComponent.BoxExtent.Z)
    local BoxLocation = UE.FVector((self.StageLength * self.StageParts / 2) - (self.StageLength / 2), BoxComponent.RelativeLocation.Y, BoxComponent.RelativeLocation.Z)
    BoxComponent:SetBoxExtent(BoxSize)
    BoxComponent:K2_SetRelativeLocation(BoxLocation, false, UE.FHitResult(), false)
    BoxComponent:SetCollisionResponseToChannel(UE.ECollisionChannel.ECC_Pawn, UE.ECollisionResponse.ECR_Block)
end

-- 更新目标位置
function M:UpdateGoalLocation(Goal)
    local Location = UE.FVector(((self.StageParts - 1) * self.StageLength + Goal.RelativeLocation.X), Goal.RelativeLocation.Y, Goal.RelativeLocation.Z)
    Goal:K2_SetRelativeLocation(Location, false, UE.FHitResult(), false)
end

return M
